require "Common/define"
require "Logic/Config"
require "Common/functions"


---@class SkinManager
SkinManager = SkinManager or {}
local this = SkinManager
local heroSkinInfo = nil
local skinCfg = nil

function SkinManager.init()
    skinCfg = G.dataTable["t_skin"]
    heroSkinInfo = {}
end

--上线就推自己所有的皮肤信息(除默认皮肤)
function SkinManager.saveSkinInfo(msg)
    if msg and msg.state == 0 then
        for _,v in pairs(msg.skins) do
            local cfig = skinCfg[tostring(v.skinId)];
            if cfig then
                local modelId = cfig.f_HeroID
                if modelId and modelId > 0 then
                    if heroSkinInfo[modelId] == nil then
                        heroSkinInfo[modelId] = {}
                    end
					v.overtime = tonumber(tostring(v.overtime))
					if not (tonumber(tostring(v.overtime)) == -1) then	-- -1是永久皮肤
						v.overtime = v.overtime * 0.001
					end
                    heroSkinInfo[modelId][v.skinId] = v
                end
            end
        end
        this.OpenTimer()
        FireEvent(Config.EventType.SkinChange)

        if not logicMgr.GuideMgr.IsGuiding() then
           -- partnerHandler.ReqHeroPower(0)
        end
    end
end

--通过英雄modelId得到该英雄的拥有的时装数据
function SkinManager.getHeroSkinSrvInfo(modelId)
    if heroSkinInfo == nil then
        heroSkinInfo = {}
    end
    return heroSkinInfo[tonumber(modelId)] or {}
end

--通过英雄modelId得到该英雄正在穿的skinId
function SkinManager.getHeroSkinId(modelId)
    local skinId = -1
    local heroSkin = heroSkinInfo[tonumber(modelId)]
    if heroSkin then
        for k,v in pairs(heroSkin) do
            if v.status == 1 then
                return v.skinId
            end
        end
    end
    --找对应默认皮肤
    for k,v in pairs(skinCfg) do
        if v.f_SkinType == 0 and v.f_HeroID == tonumber(modelId) then   --0是默认
           return v.f_SkinID
        end
    end
    --printError("未找到对应皮肤ID，请检查t_skin配表")
    return skinId
end

--通过英雄skinId得到该英雄的拥有的时装数据
function SkinManager.getHeroSkinSrvInfoBySkinId(skinId)
    local modelId = skinCfg[tostring(skinId)].f_HeroID
    if modelId and modelId > 0 and heroSkinInfo[modelId] then
        for _,v in pairs(heroSkinInfo[modelId]) do
            if v.skinId == skinId then
                return v
            end
        end
    end
    return nil
end

--通过英雄skinId得到该英雄的heroId
function SkinManager.getHeroModelIdBySkinId(skinId)
    local cfg = skinCfg[tostring(skinId)]
    if cfg then
        return cfg.f_HeroID
    else
        return 0
    end

end

--皮肤过期
function SkinManager.setSkinOutTime(skinId)
    local modelId = skinCfg[tostring(skinId)].f_HeroID
    if modelId and modelId > 0 and heroSkinInfo[modelId] then
        for k,v in pairs(heroSkinInfo[modelId]) do
            if v.skinId == skinId then
                heroSkinInfo[modelId][k] = nil
            end
        end
    end
    FireEvent(Config.EventType.SkinChange)

    if not GuideMgr.IsGuiding() then
        partnerHandler.ReqHeroPower(0)
    end
end

--皮肤穿戴
function SkinManager.setSkinWear(msg)
    if msg.state ~= 0 then
        return
    end
    local skinId = msg.skinId or 0;
    local modelId = skinCfg[tostring(skinId)].f_HeroID
    if modelId and modelId > 0 and heroSkinInfo[modelId] then
        for k,v in pairs(heroSkinInfo[modelId]) do
            if v.skinId == skinId then
                heroSkinInfo[modelId][k].status = 1
            else
                heroSkinInfo[modelId][k].status = 0
            end
        end
    end
    FireEvent(Config.EventType.SkinChange)
    if tonumber(logicMgr.HeroManager.GetRoleHeadID()) == tonumber(skinCfg[tostring(skinId)].f_HeroID) then
        logicMgr.HeroManager.SetRoleInfo("pic",toNumber(skinId))
        roleInfoHandler.SendReqModifyPicMessage(skinId);
        FireEvent(Config.EventType.FreashRoleShowDraw)
        chatHandler.SendCityPlayerInfo()
    end

    if not GuideMgr.IsGuiding() then
        partnerHandler.ReqHeroPower(0)
    end
end

--有时效的皮肤在这倒计时，到期自动刷新
function SkinManager.OpenTimer()
    if this.updateTimer then
        utimer.remove(this.updateTimer)
        this.updateTimer=nil
    end
    this.updateTimer=utimer.timeCall(function()
        for k,v in pairs(heroSkinInfo) do
            for m,n in pairs(v) do
                if n.overtime > 0 and n.overtime - HelperFunc.GetServerTime() < 0 then
                    heroSkinInfo[k][m] = nil
                    FireEvent(Config.EventType.SkinChange)
                    if tonumber(logicMgr.HeroManager.GetRoleHeadID()) == k then
                        local defaultSkinId = this.GetDefaultSkinIdByModelId(logicMgr.HeroManager.GetRoleHeadID())
                        logicMgr.HeroManager.SetRoleInfo("pic",toNumber(defaultSkinId))
                    end
                    
                    if not GuideMgr.IsGuiding() then
                        partnerHandler.ReqHeroPower(0)
                    end
                end
            end
        end
    end,2000,-1)
end

--皮肤变动时，当外部展示形象就是这个英雄时要刷新，否则不刷节省性能
function SkinManager.CheckRefreshShowHero(newWearSkinId)
    local curShowHeroId = logicMgr.HeroManager.GetRoleHeadID()
    local newWearSkinHeroModelId = skinCfg[tostring(newWearSkinId)].f_HeroID or 0
    if tonumber(newWearSkinHeroModelId) == tonumber(curShowHeroId) then
        return true
    end
    return false
end

-------------------根据skinId找t_skin对应头像、半身头像、立绘、小人------------------------
--找头像
function SkinManager.GetHeroIconBySkinId(skinId)
    local path = ""
    if skinId and tonumber(skinId) > 0 then
        local cfg = skinCfg[tostring(skinId)];
        if not cfg then
            path = dataTable.getDataTableByStringId("t_hero",skinId,"f_HeroIcon")
            if path then
                return EnumConst.AtlasPath.ICONAtlas..path
            else
                printError("t_skin表未配置，skinId"..skinId)
                return ""
            end
        end
        path = cfg.f_HeroIcon or ""
    end
    return EnumConst.AtlasPath.ICONAtlas .. path
end

function SkinManager.GetHeroIconBySkinId1(skinId)
    local path = ""
    if skinId and tonumber(skinId) > 0 then
        local cfg = skinCfg[tostring(skinId)];
        if not cfg then
            path = dataTable.getDataTableByStringId("t_hero",skinId,"f_HeroIcon")
            if path then
                return path
            else
                printError("t_skin表未配置，skinId"..skinId)
                return ""
            end
        end
        path = cfg.f_HeroIcon or ""
    end
    return path
end

--找半身头像
function SkinManager.GetHeroBIconBySkinId(skinId, modelId)
    local path = ""
    --if skinId and tonumber(skinId) > 0 then
    local cfg = skinCfg[tostring(skinId)];
    if not cfg then
        path = logicMgr.HeroManager.GetHeroBIcon(modelId)
        if path then
            return path
        else
            printError("t_skin表未配置，skinId"..skinId)
            return ""
        end
    end
    if AppConst.drawingState == 0 then
        path = cfg.f_HeroBIcon or ""  --普通模式
    else
        path = cfg.f_HeroBIcon_PA or ""  --和谐模式
    end
    --end
    return path
end

--找立绘
function SkinManager.GetHeroDrawPathBySkinId(skinId)
    local path = ""
    if skinId and tonumber(skinId) > 0 then
        local cfg = skinCfg[tostring(skinId)];
        if not cfg then
            printError("t_skin表未配置，skinId"..skinId)
            return ""
        end
        path = "Model/Drawing/" .. cfg.f_DrawID
        if AppConst.drawingState == 0 then
            return path  --普通模式
        else
            if cfg.f_DrawID_PA ~= "" then
                path = "Model/Drawing_PA/"..cfg.f_DrawID_PA  --和谐模式
            end
        end
    end
    return path
end

--找小人
function SkinManager.GetHeroModelPathBySkinId(skinId, modelId)
    local path = ""
    --if skinId and tonumber(skinId) > 0 then
    local cfg = skinCfg[tostring(skinId)];
    if not cfg then
        path = logicMgr.HeroManager.GetHeroDrawingRolePath(modelId)
        if path then
            return path
        else
            printError("t_skin表未配置，skinId"..skinId)
            return ""
        end
    end
    path = "Model/Role/" .. cfg.f_ModelID
    if AppConst.drawingState == 0 then
        return path  --普通模式
    else
        if cfg.f_ModelID_PA ~= "" then
            path = "Model/Role_PA/"..cfg.f_ModelID_PA  --和谐模式
        end
    end
    --end
    return path
end

--根据modelId找到默认皮肤
function SkinManager.GetDefaultSkinIdByModelId(modelId)
    if modelId and tonumber(modelId) then
        local data = logicMgr.HeroManager.SkinData(modelId)
        for k,v in pairs(data) do
            if v.f_SkinType == 0 then   --0是默认
                return v.f_SkinID
            end
        end
    end
end

--根据skinId找到皮肤摆放大小方向和位置信息
function SkinManager.GetSkinRankSize(skinId)
    local cfgData = skinCfg[tostring(skinId)]
    if cfgData then
        local resize = decodeJsonStrToTabel(cfgData.f_Ranksize);
        local location = decodeJsonStrToTabel(cfgData.f_RankLocation);
        local offset = decodeJsonStrToTabel(cfgData.f_RankPos);
        return resize, location, offset
    end
end

function SkinManager.OnDestroy()
    if this.updateTimer then
        utimer.remove(this.updateTimer)
        this.updateTimer = nil
    end
end

return SkinManager